#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.handlers

rhandler = logging.handlers.RotatingFileHandler(
    '/var/log/ma_sister.log', mode="wa", maxBytes=10*1024*2014, backupCount=10
)
formatter = logging.Formatter(
    '%(asctime)s %(levelname)-7s %(message)s', '%Y %b %d %H:%M:%S'
)
rhandler.setFormatter(formatter)
LOG = logging.getLogger()
LOG.setLevel(logging.INFO)
LOG.addHandler(rhandler)

import requests
requests.adapters.DEFAULT_RETRIES = 5
s = requests.session()
s.keep_alive = False

from bs4 import BeautifulSoup

OPENSTACK_VERSION = "queens"
CURRENT = "deliverables/%s" % OPENSTACK_VERSION
INDEPENDENT = "deliverables/_independent"

ROOT_PATH = "/root"
MA_SISTER_PATH = "%s/ma_sister" % ROOT_PATH
RPM_PACKAGING_PATH = "%s/rpm-packaging" % ROOT_PATH
J2_PATH = "%s/openstack/%s/%s"

RPM_PACKAGING_PATCH_URL = "https://review.openstack.org/changes/?q=project:openstack/rpm-packaging+status:open&n=75&O=81"
RELEASE_PATCH_URL = "https://review.openstack.org/changes/?q=project:openstack/releases+status:open&n=25&O=81"
RPM_PACKAGING_URL = "https://git.openstack.org/openstack/rpm-packaging"
RELEASES_INDEX_URL = "https://releases.openstack.org/%s/index.html" % OPENSTACK_VERSION

COMMIT_FILE = "https://review.openstack.org/changes/%s/revisions/1/files"
DIFF_FILE = "https://review.openstack.org/changes/{}/revisions/1/files/deliverables%2F{}%2F{}/diff?context=ALL&intraline&whitespace=IGNORE_NONE"
PATCH_FILE = "%s/patch.json" % ROOT_PATH


class TimeoutError(Exception):
    pass


class GitReviewError(Exception):
    pass


class Project(object):
    def __init__(self, project_name, old_version, new_version, depend_id=None):
        self._name = project_name
        self._old = old_version
        self._new = new_version
        self._id = depend_id

    @property
    def name(self):
        return self._name

    @property
    def old(self):
        return self._old

    @property
    def new(self):
        return self._new

    @property
    def depend_id(self):
        return self._id

    def __repr__(self):
        return "%s changed version %s to %s, depend:%s" % (self._name, self._old, self._new, self._id)

    def __hash__(self):
        return hash("%s:%s" % (self.name, self.new))
